﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media.Animation;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Content;

namespace FeedTheWalrus
{
    public class Fish
    {
        public int colorOfFish;
        Vector2 position;
        TimeSpan lifeSpan;
        TimeSpan totalLifeSpan = new TimeSpan(0, 0, 10);
        Vector2 velocity;
        public bool dead = false;
        ThunderFish.Sprite fish;
        ThunderFish.SpriteSheet spriteSheet;

        private const float speed = 100f;

        public Fish()
        {
            colorOfFish = Walrus.fishes.Next(Walrus.FishColorCount);

            position = new Vector2(
                Walrus.fishes.Next(SharedGraphicsDeviceManager.Current.GraphicsDevice.DisplayMode.Width - 80) + 80,
                Walrus.fishes.Next(SharedGraphicsDeviceManager.Current.GraphicsDevice.DisplayMode.Height - 200));

            velocity = new Vector2(Walrus.fishes.Next(-5, 5), Walrus.fishes.Next(-5, 5));
            velocity.Normalize();
            velocity *= speed;

            lifeSpan = totalLifeSpan;

            fish = new ThunderFish.Sprite(FishManager.fishSpriteSheet);
            fish.FrameLength = new TimeSpan(0, 0, 0, 0, 500);
            fish.LoopAnimation();
            fish.CenterOffset();
        }

        public void OnUpdate(GameTimerEventArgs e)
        {
            lifeSpan -= e.ElapsedTime;
            if (lifeSpan <= TimeSpan.Zero)
            {
                dead = true;
            }

            fish.Update(e.ElapsedTime);
            fish.Position = position;

            position.X += velocity.X * (float)e.ElapsedTime.TotalSeconds;
            position.Y += velocity.Y * (float)e.ElapsedTime.TotalSeconds;

            if (position.X <= 0)
            {
                velocity.X *= -1;
            }
            else if (position.X >= SharedGraphicsDeviceManager.Current.GraphicsDevice.DisplayMode.Width)
            {
                velocity.X *= -1;
            }
            else if (position.Y <= 80)
            {
                velocity.Y *= -1;
            }
            else if (position.Y >= SharedGraphicsDeviceManager.Current.GraphicsDevice.DisplayMode.Height - 200)
            {
                velocity.Y *= -1;
            }

        }

        public void OnNavigatedFrom()
        {

        }


        public bool Contains(Vector2 point)
        {
            return fish.CollisionBox.Intersects(new Rectangle((int)point.X - 10, (int)point.Y - 10, 20,20));
        }


        public void OnDraw(SpriteBatch sb, GameTimerEventArgs e)
        {
            fish.Draw(
                sb,
                fish.Position - fish.Offset,
                Walrus.colors[colorOfFish]);
        }

    }
}
